using System;
using System.Collections;
using System.Collections.Generic;
using MyJson;
namespace Pomelo.Protobuf
{
    public class Encoder
    {

        //Encode the UInt32.
        //public static byte[] encodeUInt32(string n)
        //{
        //    return encodeUInt32(Convert.ToUInt32(n));
        //}

        /// <summary>
        /// Encode the UInt32.
        /// </summary>
        /// <returns>
        /// byte[]
        /// </returns>
        /// <param name='n'>
        /// int
        /// </param>
        public static byte[] encodeUInt32(uint n)
        {
           
            //
            List<byte> byteList = new List<byte>();
            do
            {
                uint tmp  = n % 128;
                uint next = n >> 7;
                if (next != 0)
                {
                    tmp = tmp + 128;
                }
                byteList.Add(Convert.ToByte(tmp));
                n = next;
            } while (n != 0);

            return byteList.ToArray();
        }

        /// <summary>
        /// Encode the UInt32.
        /// </summary>
        /// <returns>
        /// byte[]
        /// </returns>
        /// <param name='n'>
        /// int
        /// </param>
        public static byte[] encodeUInt64(UInt64 n)
        {
      
            //
            List<byte> byteList = new List<byte>();
            do
            {
                UInt64 tmp = n % 128;
                UInt64 next = n >> 7;
                if (next != 0)
                {
                    tmp = tmp + 128;
                }
                byteList.Add(Convert.ToByte(tmp));
                n = next;
            } while (n != 0);

            return byteList.ToArray();
        }

        /// <summary>
        /// Encodes the SInt32.
        /// </summary>
        /// <returns>
        /// byte []
        /// </returns>
        /// <param name='n'>
        /// int
        /// </param>
        public static byte[] encodeSInt32(int n)
        {
            //   UInt32 num = (uint)(n < 0 ? (Math.Abs(n) * 2 - 1) : n * 2);
            // uint num = Convert.ToUInt32(n);
            return encodeUInt64((uint)n);
        }

        /// <summary>
        /// Encodes the float.
        /// </summary>
        /// <returns>
        /// byte []
        /// </returns>
        /// <param name='n'>
        /// float.
        /// </param>
        public static byte[] encodeFloat(float n)
        {
            byte[] bytes = BitConverter.GetBytes(n);
            if (!BitConverter.IsLittleEndian)
            {
                Util.Reverse(bytes);
            }
            return bytes;
        }

        //Get the byte length of message.
        public static int byteLength(string msg)
        {
            return System.Text.Encoding.UTF8.GetBytes(msg).Length;
        }


    }
}